﻿using System;
using System.Reflection;
using System.Threading.Tasks;

namespace GGetTestService.Extension
{
    /// <summary>
    /// obj扩展方法
    /// </summary>
    public static class ObjExtensions
    {
        /// <summary>
        /// obj转为int类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static int ObjToInt(this object thisValue)
        {
            int reval = 0;
            if (thisValue == null) return 0;
            if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
            {
                return reval;
            }
            return reval;
        }

        /// <summary>
        /// obj转为Guid类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static Guid ObjToGuid(this object thisValue)
        {
            Guid reval = Guid.Empty;
            if (thisValue == null) return reval;
            if (thisValue != null && thisValue != DBNull.Value && Guid.TryParse(thisValue.ToString(), out reval))
            {
                return reval;
            }
            return reval;
        }

        /// <summary>
        ///obj转为double类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static double ObjToDouble(this object thisValue)
        {
            double reval = 0;
            if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
            {
                return reval;
            }
            return 0;
        }

        /// <summary>
        ///obj转为decimal类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static decimal ObjToDecimal(this object thisValue)
        {
            decimal reval = 0;
            if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
            {
                return reval;
            }
            return 0;
        }

        /// <summary>
        ///obj转为string类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static string ObjToString(this object thisValue)
        {
            if (thisValue != null) return thisValue.ToString().Trim();
            return string.Empty;
        }

        /// <summary>
        ///obj转为datetime类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static DateTime ObjToDate(this object thisValue)
        {
            DateTime reval = DateTime.MinValue;
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
            {
                reval = Convert.ToDateTime(thisValue);
            }
            return reval;
        }

        /// <summary>
        /// 将时间转化为yyyy-MM-dd HH:mm:ss 格式的字符串模式
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="isRemoveHourMinuteSecond">是否移除十分秒</param>
        /// <returns></returns>
        public static string ObjToDateStringFormat(this object thisValue, bool isRemoveHourMinuteSecond = false)
        {
            DateTime reval = DateTime.MinValue;
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
            {
                reval = Convert.ToDateTime(thisValue);
            }
            if (isRemoveHourMinuteSecond)
                return reval.ToString("yyyy-MM-dd");
            return reval.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 将时间转化为中文格式，不带时分秒，格式："yyyy年MM月dd日"
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="isRemoveHourMinuteSecond">是否移除十分秒</param>
        /// <returns></returns>
        public static string ObjToChineseDateString(this object thisValue, bool isRemoveHourMinuteSecond = false)
        {
            DateTime reval = DateTime.MinValue;
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
            {
                reval = Convert.ToDateTime(thisValue);
            }
            if (isRemoveHourMinuteSecond)
                return reval.ToString($"{reval.Year}年{reval.Month}月{reval.Day}日");
            return reval.ToString($"{reval.Year}年{reval.Month}月{reval.Day}日 {reval.Hour}时{reval.Minute}分{reval.Second}秒");
        }

        /// <summary>
        ///obj转化为bool类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static bool ObjToBool(this object thisValue)
        {
            bool reval = false;
            if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
            {
                return reval;
            }
            return reval;
        }

        /// <summary>
        /// 获取当前时间的时间戳
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static string DateToTimeStamp(this DateTime thisValue)
        {
            TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 将泛型对象中string的属性做trim处理
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static TEntity ObjToTrim<TEntity>(this TEntity entity) where TEntity : class, new()
        {
            Type t = entity.GetType();
            PropertyInfo[] props = t.GetProperties();
            Parallel.ForEach(props, p =>
            {
                if (string.Equals(p.PropertyType.Name, nameof(String), StringComparison.OrdinalIgnoreCase))
                {
                    var tmp = (string)p.GetValue(entity);
                    if (tmp != null)
                    {
                        p.SetValue(entity, tmp.Trim(), null);
                    }
                }
            });
            return entity;
        }
    }
}